package com.xh.bussiness.exam.bean.dto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.collections4.MapUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * </p>
 *
 * @author Pitt zhang
 * @since 2019/11/6
 */
@Data
@ApiModel("小程序学生报告返回对象")
public class StudentReportForAppletDto implements Serializable {

    private static final long serialVersionUID = 7807826206744092949L;

    @ApiModelProperty("学段和学科统计")
    private List<GradeRangeSubjectErrorQuestionBean> gradeRangeSubjectCount = new ArrayList<>();

    @ApiModelProperty("薄弱知识点统计")
    private List<ObjectBean> knowledgeCount = new ArrayList<>();

    @ApiModelProperty("错误原因统计")
    private List<ObjectBean> errorReasonCount = new ArrayList<>();

    public void calc(List<Map<String, Object>> userErrorQuestions, List<Map<String, String>> knowledgeAnalyses) {
        //学段和学科统计
        if (!CollectionUtils.isEmpty(userErrorQuestions)) {
            Map<String, List<Map<String, Object>>> gradeRangeSubjectMap = userErrorQuestions.stream().collect(Collectors.groupingBy((item -> (item.get("gradeRange") + item.get("subject").toString()))));
            for (String key : gradeRangeSubjectMap.keySet()) {
                List<Map<String, Object>> values = gradeRangeSubjectMap.get(key);
                if (!CollectionUtils.isEmpty(values)) {
                    GradeRangeSubjectErrorQuestionBean bean = new GradeRangeSubjectErrorQuestionBean();
                    Object gradeRangeName = values.get(0).get("gradeRangeName");
                    Object subjectName = values.get(0).get("subjectName");
                    if (gradeRangeName != null && subjectName != null) {
                        bean.setGradeRangeSubjectName(gradeRangeName.toString() + subjectName.toString());
                        //订正数
                        long correctCount = values.stream().filter(item -> item.get("status").toString().equals("1")).count();
                        //错题数
                        long errorCount = values.stream().filter(item -> item.get("status").toString().equals("0")).count();
                        bean.setErrorQuestionCount(errorCount);
                        bean.setCorrectQuestionCount(correctCount);
                    }
                    gradeRangeSubjectCount.add(bean);
                }
            }

            //错误原因统计
            Map<String, ObjectBean> reasonsCountMap = new HashMap<>();
            for (Map<String, Object> userErrorQuestion : userErrorQuestions) {
                Object errorReason = userErrorQuestion.get("errorReason");
                if (errorReason != null) {
                    //拆分错误原因 累加错题数量
                    String[] errorReasons = errorReason.toString().split(",");
                    for (String reason : errorReasons) {
                        if (reasonsCountMap.containsKey(reason)) {
                            ObjectBean objectBean = reasonsCountMap.get(reason);
                            int newCount = objectBean.getData() + 1;
                            objectBean.setData(newCount);
                            reasonsCountMap.put(reason, objectBean);
                        } else {
                            ObjectBean objectBean = new ObjectBean();
                            objectBean.setName(reason);
                            objectBean.setData(1);
                            reasonsCountMap.put(reason, objectBean);
                        }
                    }
                }
            }
            if (!MapUtils.isEmpty(reasonsCountMap)) {
                errorReasonCount.addAll(reasonsCountMap.values());
            }
        }
        //薄弱知识点分析
        if (!CollectionUtils.isEmpty(knowledgeAnalyses)) {
            Map<String, ObjectBean> knowledgeCountMap = new HashMap<>();
            for (Map<String, String> map : knowledgeAnalyses) {
                String parentPath = map.get("parentPath");
                String parentPathName = map.get("parentPathName");
                if (StringUtils.isEmpty(parentPath) || StringUtils.isEmpty(parentPathName)) {
                    continue;
                }
                String[] parentIds = parentPath.split("-");
                String[] parentPathNames = parentPathName.split("-");
                try {
                    //获取根级知识点
                    String rootId = parentIds[1];
                    String rootName = parentPathNames[1];
                    if (StringUtils.isEmpty(rootId) || StringUtils.isEmpty(rootName)) {
                        continue;
                    }
                    if (knowledgeCountMap.containsKey(rootId)) {
                        ObjectBean objectBean = knowledgeCountMap.get(rootId);
                        int newCount = objectBean.getData() + 1;
                        objectBean.setData(newCount);
                        knowledgeCountMap.put(rootId, objectBean);
                    } else {
                        ObjectBean objectBean = new ObjectBean();
                        objectBean.setId(rootId);
                        objectBean.setName(rootName);
                        objectBean.setData(1);
                        knowledgeCountMap.put(rootId, objectBean);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (!MapUtils.isEmpty(knowledgeCountMap)) {
                knowledgeCount.addAll(knowledgeCountMap.values());
            }
        }


    }

    @Data
    @ApiModel("按学科统计错题数情况")
    static class GradeRangeSubjectErrorQuestionBean {
        @ApiModelProperty("学科名称")
        private String gradeRangeSubjectName;
        @ApiModelProperty("错题数")
        private Long errorQuestionCount;
        @ApiModelProperty("订正数")
        private Long correctQuestionCount;
    }

    @Data
    @ApiModel("学段和学科错题数统计")
    static class ObjectBean {
        @ApiModelProperty("id")
        private String id;

        @ApiModelProperty("名称")
        private String name;

        @ApiModelProperty("数量")
        private Integer data;
    }
}



